//
//  UpshiftHeadController.m
//  基本框架
//
//  Created by runone02 on 2018/8/6.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "UpshiftHeadController.h"
#import "LQYMineTableViewCell.h"
#import "upShiftNavView.h"
#import "upShiftHeadView.h"
#define headImgWidth W_RATIO(30)


@interface UpshiftHeadController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
/** */
@property (nonatomic, strong) UITableView *mainTableView;

/** */
@property (nonatomic, assign) CGFloat lastOffsetY;

/** */
@property (nonatomic, assign) CGFloat needMoveY;//Y方向可移动的距离
@property (nonatomic, assign) CGFloat MoveMultiple;//Y方向每移动一个像素，headImg的X方向需移动的倍数
@property (nonatomic, assign) CGFloat widthMultiple;//头像的宽度倍数
@property (nonatomic, strong) NSArray *dataArr;
/** */
@property (nonatomic, strong) upShiftNavView * nav;
/** */
@property (nonatomic, strong) upShiftHeadView *headView;
@end

@implementation UpshiftHeadController
static NSString *mineTableCellID = @"LQYMineTableViewCell";

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadTableView];
    
}


- (void)loadTableView{
    self.dataArr = @[@[
                         @{FindCellImageName:@"MoreMyBankCard", FindCellContentStr:@"指数"},
                         @{FindCellImageName:@"MoreMyBankCard", FindCellContentStr:@"学习中心"},
                         @{FindCellImageName:@"MoreMyBankCard", FindCellContentStr:@"福利"},
                         @{FindCellImageName:@"MoreExpressionShops", FindCellContentStr:@"表情"},
                         @{FindCellImageName:@"MoreMyFavorites", FindCellContentStr:@"收藏"},
                         @{FindCellImageName:@"ff_IconShowAlbum", FindCellContentStr:@"相册"},
                         @{FindCellImageName:@"MyCardPackageIcon", FindCellContentStr:@"卡包"},
                         ],
                     
                     @[@{FindCellImageName:@"MoreMyFavorites", FindCellContentStr:@"收藏"},
                       @{FindCellImageName:@"ff_IconShowAlbum", FindCellContentStr:@"相册"},
                       @{FindCellImageName:@"MyCardPackageIcon", FindCellContentStr:@"卡包"},
                       ],
                     
                     @[@{FindCellImageName:@"MoreSetting", FindCellContentStr:@"设置"}]
                     ];
    
    self.mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
    self.mainTableView.backgroundColor = [UIColor whiteColor];
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    NSArray *Animation_ImageArray = [NSArray arrayWithObjects:IMAGE(@"Animation_1"), IMAGE(@"Animation_2"),IMAGE(@"Animation_3"),IMAGE(@"Animation_2"), nil];
    [header setImages:Animation_ImageArray forState:MJRefreshStateRefreshing];

    // 设置header
    self.mainTableView.mj_header = header;
    [self.view addSubview:self.mainTableView];

    self.headView = [[upShiftHeadView alloc]init];
    [self.view addSubview:self.headView];
    self.headView.height = 160;
    [self createNavView];
    self.mainTableView.tableHeaderView = self.headView;
}


#pragma mark - createView
- (void)createNavView{
    self.nav = [[upShiftNavView alloc]init];
    [self.view addSubview:self.nav];
    [self.nav mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(kTopBarHeight);
    }];    
    //    创建完所有的东西之后计算需要移动的位置和距离
    [self dealPosition];
}

- (void)dealPosition{
    //头像距离左端的距离不能直接用offset来决定，因为屏幕宽度不一样，要根据屏幕宽度来计算出一个倍数
    //1、计算出总共需要向右移动多少距离
    CGFloat needMoveX = kTopBarHeight;
    //2、找出上下移动多少距离就得到位
    self.needMoveY = self.headView.height - kTopBarHeight;
    //3、根据这两个参数，计算出倍数，然后重新定义leftPadding
    self.MoveMultiple = self.needMoveY/needMoveX;
        
    //    1、首先确定初始大小和末尾大小的差
    //    初始是headImgWidth，末尾是25
    //    2、然后是Y移动的距离
    //    3、根据移动的倍数计算应该减少多少
    CGFloat subWidth = self.headView.headImg.width - self.nav.smallImgView.width;
    self.widthMultiple = (CGFloat)subWidth / self.needMoveY;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offsetY = scrollView.contentOffset.y;//计算当前偏移位置
    self.nav.navImageView.alpha = 1;
    
    CGFloat leftPadding = offsetY/self.MoveMultiple + W_RATIO(60);//头像距左端原有25像素；moveX = moveY/倍数;将x设置给headImg；
    CGFloat headWidth = headImgWidth - offsetY*self.widthMultiple;//计算头像移动过程中的宽度
    
    if (offsetY > self.needMoveY) {//和nav重合的状态，直接显示navImageView
        //        1、设置navImageView的背景图片；小头像出现
        self.nav.navImageView.image = IMAGE(@"mineNavBar");
        self.nav.smallImgView.image = IMAGE(@"gestureHead");
        
    }else if (offsetY <= 0) {//说明是下拉，隐藏 navView
        CGFloat imgAlpha = 1 + offsetY/40;//下拉40个像素就完全隐藏
        self.nav.navImageView.alpha = imgAlpha;
        self.headView.headImg.frame = CGRectMake(25, self.headView.HeadImageView.height-13-headImgWidth, headImgWidth, headImgWidth);

        
    }else{//移动过程
        self.nav.navImageView.image = IMAGE(@"");
        self.nav.smallImgView.image = IMAGE(@"");
        self.headView.headImg.frame = CGRectMake(leftPadding, self.headView.HeadImageView.height-13-headWidth, headWidth, headWidth);

    }
}


#pragma mark -tablviewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArr[section] count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    LQYMineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mineTableCellID];
    if (cell == nil) {
        cell = [[LQYMineTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:mineTableCellID];
        [cell setContentData:[self.dataArr[indexPath.section] objectAtIndex:indexPath.row]];
    }
    return cell;
}

#pragma mark -BtnClicked
- (void)loadData{//    下拉刷新
    self.nav.navImageView.alpha = 0;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.mainTableView.mj_header endRefreshing];
    });
}

@end
